import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout/Layout.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },

  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'user',
        redirect: '/user/list',
        name: 'User',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '用户管理' },
        children: [
          {
            path: 'list',
            component: () => import('@/views/user/UserList'),
            meta: { title: '用户列表' }
          },
          {
            path: 'add',
            component: () => import('@/views/user/UserEdit'),
            meta: { title: '新增用户' }
          },
          {
            path: 'update/:id',
            name: 'updateUser',
            component: () => import('@/views/user/UserEdit'),
            meta: { title: '修改用户' },
            props: true
          }
        ]
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/user/role.vue'),
        meta: { title: '角色管理' }
      },
      {
        path: 'products',
        component: () => import('@/views/products/index.vue'),
        children: [
          {
            path: '',
            name: 'Products',
            component: () => import('@/views/products/list.vue'),
            meta: { title: '商品管理', icon: 'el-icon-goods' }
          },
          {
            path: 'add',
            name: 'ProductAdd',
            component: () => import('@/views/products/add'),
            meta: { title: '新增商品', icon: 'el-icon-plus' },
            hidden: true
          },
          {
            path: 'edit/:id',
            name: 'ProductEdit',
            component: () => import('@/views/products/edit'),
            meta: { title: '编辑商品', icon: 'el-icon-edit' },
            hidden: true
          }
        ]
      },
      {
        path: 'categories',
        name: 'Categories',
        component: () => import('@/views/products/categories.vue'),
        meta: { title: '分类管理' }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('@/views/orders/index.vue'),
        meta: { title: '订单列表' }
      },
      {
        path: 'purchase',
        name: 'Purchase',
        component: () => import('@/views/purchase/index.vue'),
        meta: { title: '采购管理' },
        children: [
          {
            path: 'goods',
            name: 'PurchaseGoods',
            component: () => import('@/views/purchase/goods.vue'),
            meta: { title: '采购商品' }
          },
          {
            path: 'orders',
            name: 'PurchaseOrders',
            component: () => import('@/views/purchase/orders.vue'),
            meta: { title: '采购订单' }
          }
        ]
      },
      {
        path: 'supplier',
        name: 'Supplier',
        component: () => import('@/views/supplier/index.vue'),
        meta: { title: '供应商运营' },
        children: [
          {
            path: 'list',
            name: 'SupplierList',
            component: () => import('@/views/supplier/list.vue'),
            meta: { title: '供应商列表' }
          },
          {
            path: 'settlement',
            name: 'Settlement',
            component: () => import('@/views/supplier/settlement.vue'),
            meta: { title: '结算记录' }
          },
          {
            path: 'add',
            name: 'SupplierAdd',
            component: () => import('@/views/supplier/edit.vue'),
            meta: { title: '新增供应商', hidden: true }
          },
          {
            path: 'edit/:id',
            name: 'SupplierEdit',
            component: () => import('@/views/supplier/edit.vue'),
            meta: { title: '编辑供应商', hidden: true },
            props: true
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn');

  console.log('是否登录', isLoggedIn);

  if (to.path === '/login') {
    if (isLoggedIn && isLoggedIn !== 'null') {
      next('/home');
    } else {
      next();
    }
  } else {
    if (isLoggedIn && isLoggedIn !== 'null') {
      next();
    } else {
      next('/login');
    }
  }
});


export default router
